//
//  ViewController.m
//  WangYiTest
//
//  Created by 袁飞霞 on 16/5/10.
//  Copyright © 2016年 cn.sophietobeabetterone. All rights reserved.
//

#import "ViewController.h"
#import "FXLabel.h"
#import "FXChannel.h"
#import "FXCollectionViewCell.h"
#import "FXNewsViewController.h"

@interface ViewController ()<UICollectionViewDataSource,UICollectionViewDelegate>

@property (nonatomic, weak) UIScrollView *scrollView;
@property (nonatomic, weak) FXLabel *selectedLabel;
@property (nonatomic, weak) UICollectionView *collectionView;
@property (nonatomic, strong) UICollectionViewFlowLayout *layout;
@property (nonatomic, strong) NSArray *channels;
@property (nonatomic, strong) NSMutableDictionary *controllerCache;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    UIScrollView *scrollView = [[UIScrollView alloc]init];
    self.scrollView = scrollView;
    scrollView.frame = CGRectMake(0, 20, [UIScreen mainScreen].bounds.size.width, 21);
    [self.view addSubview:scrollView];
    
    [self setupScrollView];
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc]init];
    
    UICollectionView *collectionView = [[UICollectionView alloc]initWithFrame:CGRectZero collectionViewLayout:layout];
    self.collectionView = collectionView;
    self.layout = layout;
    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;
    
}
-(void)setupScrollView
{
    
    int index = (int)self.channels.count;
  
    
    for (int i = 0; i < index; ++i) {
        FXChannel *channel = self.channels[i];
        
        FXLabel *label= [[FXLabel alloc]initWithName:channel.tname];
        __weak typeof(label)weakLabel = label;
        __weak typeof(self) weakVc = self;
        [self.scrollView addSubview:label];
        weakLabel.labelSelected = ^{
            self.selectedLabel.scale = 0;
            weakLabel.scale = 1;
            weakVc.selectedLabel = weakLabel;
        };
             }
    self.selectedLabel = self.scrollView.subviews[0];
    self.selectedLabel.scale = 1;

}
//布局scrollView中的标签位置 因为scrollView 滚动时的触摸和按钮的点击会冲突,因此我们里面放的是label而不是button
-(void)viewDidLayoutSubviews
{
    [super viewDidLayoutSubviews];
    CGFloat marginX = 8;
//    CGFloat labelW = 60;
    CGFloat labelH = 21;
    CGFloat __block labelX = marginX;
    [self.scrollView.subviews enumerateObjectsUsingBlock:^(__kindof FXLabel * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        
        obj.frame  = CGRectMake(labelX , 0, obj.frame.size.width, labelH);
        labelX += obj.frame.size.width ;
    }];
    
    self.scrollView.contentSize = CGSizeMake(labelX , labelH);
    self.scrollView.showsHorizontalScrollIndicator = NO;
    
}
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 1;
    
}
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.channels.count;
}
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *ID=@"channelCell";
    FXCollectionViewCell *cell=[collectionView dequeueReusableCellWithReuseIdentifier:ID forIndexPath:indexPath];
    
    //获取数据模型
    FXChannel *channel = self.channels[indexPath.item];
    FXNewsViewController *VC = [self newVCWith:channel.URLString];
    //对cell赋值
    cell.newsVC = VC;
    return cell;
}

//取得对应控制器
-(FXNewsViewController *)newVCWith:(NSString *)urlStr
{
    FXNewsViewController * newVC =  self.controllerCache[urlStr];
    if (!newVC) {
//       FXNewsViewController *newVC =
        [self.controllerCache setValue:newVC forKey:urlStr];
        
    }

    return newVC;
}

-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    
}


-(NSArray *)channels
{
    if (_channels == nil) {
        _channels = [FXChannel channels];
    }
    return _channels;
}

@end
